package sdstcom.lbq.one.lbq.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.ViewUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.VideoView;

import com.example.antivity.MovieActivity;
import com.example.antivity.MusicActivity;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.LinkedList;

import sdstcom.lbq.one.R;
import sdstcom.lbq.one.lbq.Databeans.Videobean;
import sdstcom.lbq.one.lbq.adapter.VideoAdapter;

/**
 * Created by Administrator on 2017/1/9 0009.
 */

public class Videofragment extends Fragment implements View.OnClickListener {

    private View view;
    private Button search;
    private PullToRefreshListView plv;
    private String id;
    private LinkedList<Videobean.DataBean> list;
    private VideoAdapter adapter;
    public static Videofragment Initfragment()
    {
        Videofragment videofragment=new Videofragment();
        return videofragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         view=inflater.inflate(R.layout.videofragment_item,container,false);
         search= (Button) view.findViewById(R.id.video_search);
        search.setOnClickListener(this);
        plv= (PullToRefreshListView) view.findViewById(R.id.video_listview);
         plv.setMode(PullToRefreshBase.Mode.BOTH);
         list=new LinkedList<>();
         adapter=new VideoAdapter(getActivity(),list);
         plv.setAdapter(adapter);
         RequestParams params=new RequestParams("http://v3.wufazhuce.com:8000/api/channel/movie/more/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91");
        params.setConnectTimeout(5000);
         x.http().get(params, new Callback.CommonCallback<String>() {

             @Override
             public void onSuccess(String result) {
                 Videobean bean=new Gson().fromJson(result,Videobean.class);
                 list.addAll(bean.getData());
                 id=list.get(list.size()-1).getId();
                 adapter.notifyDataSetChanged();
             }

             @Override
             public void onError(Throwable ex, boolean isOnCallback) {
                 Toast.makeText(getActivity(),ex.toString(),Toast.LENGTH_SHORT).show();
             }

             @Override
             public void onCancelled(CancelledException cex) {

             }

             @Override
             public void onFinished() {

             }
         });
        plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                loaddata("0");

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                loaddata(id);


            }
        });
        plv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                startActivity(new Intent(getActivity(), MovieActivity.class).putExtra("itemid",list.get(position-1).getItem_id()));
            }
        });

        return view;
    }

    private void loaddata(String i) {
        if (i.equals("0"))
        {
            RequestParams params=new RequestParams("http://v3.wufazhuce.com:8000/api/channel/movie/more/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91");
            params.setConnectTimeout(5000);
            x.http().get(params, new Callback.CommonCallback<String>() {

                @Override
                public void onSuccess(String result) {
                    Videobean bean=new Gson().fromJson(result,Videobean.class);
                    if (!bean.getData().get(bean.getData().size() - 1).getId().equals(id))
                    {
                        list.addAll(0, bean.getData());
                        adapter.notifyDataSetChanged();

                    }
                    plv.onRefreshComplete();
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {  plv.onRefreshComplete();

                }

                @Override
                public void onCancelled(CancelledException cex) {
                    plv.onRefreshComplete();
                }

                @Override
                public void onFinished() {
                    plv.onRefreshComplete();
                }
            });



        }
        else
        {

            RequestParams params=new RequestParams("http://v3.wufazhuce.com:8000/api/channel/movie/more/"+id+"?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91");
            params.setConnectTimeout(5000);
            x.http().get(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Videobean bean=new Gson().fromJson(result,Videobean.class);
                    list.addAll(bean.getData());
                    id = bean.getData().get(bean.getData().size() - 1).getId();
                    adapter.notifyDataSetChanged();
                    plv.onRefreshComplete();

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    plv.onRefreshComplete();
                }

                @Override
                public void onCancelled(CancelledException cex) {
                    plv.onRefreshComplete();
                }

                @Override
                public void onFinished() {
                    plv.onRefreshComplete();
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(getActivity(),"搜索模块优化中。。。敬请期待",Toast.LENGTH_SHORT).show();
    }
}
